from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


class BaseAction:

    def __init__(self, driver):
        self.driver = driver

    def find_element(self, loc, time=10, poll=1):
        return WebDriverWait(self.driver, time, poll).until(lambda x: x.find_element(loc[0], loc[1]))

    def find_elements(self, loc, time=10, poll=1):
        return WebDriverWait(self.driver, time, poll).until(lambda x: x.find_elements(loc[0], loc[1]))


    def act_click(self, loc):
        # 点击
        self.find_element(loc).click()

    def act_swipe(self, loc):
        # 滑动
        return self.driver.swipe(loc[0], loc[1], loc[2], loc[3], loc[4])

    def switch_app(self):
        # 切换NATIVE
        return self.driver.switch_to.context("NATIVE_APP")

    def switch_web(self):
        # webview WEBVIEW_com.chebao
        # 切换webview
        return self.driver.switch_to.context("WEBVIEW_com.chebao")

    def act_send(self, loc, text):
        # 输入
        return self.find_element(loc).send_keys(text)

    def find_toast(self, loc, timeout=3):
        """
        # message: 预期要获取的toast的部分消息
        """
        return WebDriverWait(self.driver, timeout, 0.1).until(lambda x: x.find_element(loc[0], loc[1]))

